package org.balthie.cal.impl.calculator;

import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Ctrl_Echo;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Ctrl_Redo;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Ctrl_Undo;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.MathOpt_Add;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.MathOpt_Div;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.MathOpt_EQ;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.MathOpt_Mul;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.MathOpt_Sub;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_0;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_1;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_2;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_3;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_4;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_5;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_6;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_7;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_8;
import static org.balthie.cal.core.AbsCalculator.StandardInputerKey.Numb_9;

import java.util.Optional;

import org.balthie.cal.core.AbsCalculator;
import org.balthie.cal.core.ICommand;
import org.balthie.cal.core.IInputer;
import org.balthie.cal.core.IMathOperator;
import org.balthie.cal.impl.cmd.EchoCmd;
import org.balthie.cal.impl.cmd.EqualsCmd;
import org.balthie.cal.impl.cmd.GenericMathOptCmd;
import org.balthie.cal.impl.cmd.NumbCmd;
import org.balthie.cal.impl.cmd.NumbZeroCmd;
import org.balthie.cal.impl.cmd.RedoCmd;
import org.balthie.cal.impl.cmd.UndoCmd;
import org.balthie.cal.impl.io.EchoButton;

/**
 * @author：balthie@126.com
 * @createtime ： 2023年3月7日 下午1:37:55
 * @description 基础的计算器实现，包含 10个整数，加减乘除符号
 * @since version 初始于版本 0.0.1-SNAPSHOT
 */
public class BaseCalculator extends AbsCalculator {

	public BaseCalculator() {
		super();
		// 数字指令按键
		registGenericInputer(Numb_1.getKey(), new NumbCmd("1"));
		registGenericInputer(Numb_2.getKey(), new NumbCmd("2"));
		registGenericInputer(Numb_3.getKey(), new NumbCmd("3"));
		registGenericInputer(Numb_4.getKey(), new NumbCmd("4"));
		registGenericInputer(Numb_5.getKey(), new NumbCmd("5"));
		registGenericInputer(Numb_6.getKey(), new NumbCmd("6"));
		registGenericInputer(Numb_7.getKey(), new NumbCmd("7"));
		registGenericInputer(Numb_8.getKey(), new NumbCmd("8"));
		registGenericInputer(Numb_9.getKey(), new NumbCmd("9"));
		registGenericInputer(Numb_0.getKey(), new NumbZeroCmd("0"));

		// 运算符指令按键
		registGenericInputer(MathOpt_Add.getKey(), new GenericMathOptCmd(IMathOperator.MathOperatorType.Add));
		registGenericInputer(MathOpt_Sub.getKey(), new GenericMathOptCmd(IMathOperator.MathOperatorType.Subtract));
		registGenericInputer(MathOpt_Mul.getKey(), new GenericMathOptCmd(IMathOperator.MathOperatorType.Multiply));
		registGenericInputer(MathOpt_Div.getKey(), new GenericMathOptCmd(IMathOperator.MathOperatorType.Divide));
		registGenericInputer(MathOpt_EQ.getKey(), new EqualsCmd());

		// 控制指令按键
		registGenericInputer(Ctrl_Undo.getKey(), new UndoCmd());
		registGenericInputer(Ctrl_Redo.getKey(), new RedoCmd());
		bindCmd(new EchoButton(Ctrl_Echo.getKey(), this), new EchoCmd());
	}

	public BaseCalculator addButtonAlias(StandardInputerKey key, String alias) {
		IInputer iInputer = this.keyboard.get(key.getKey());
		ICommand iCommand = commandDict.get(iInputer);
		registGenericInputer(alias, iCommand);
		return this;
	}
	
	public Optional<IInputer> getInputer(String inputerName) {
		IInputer iInputer = super.keyboard.get(inputerName);
		return Optional.ofNullable(iInputer);
	}
}
